import {
  Body,
  Controller,
  Get,
  Inject,
  Post,
  Provide,
  Query,
  ALL,
  Put,
  Param,
  Del,
} from '@midwayjs/decorator';
import { DictManageDTO } from '../dto/dictManage';
import { DictManageService } from '../service/dictManage';

@Provide()
@Controller('/dict')
export class DictManageController {
  @Inject()
  dictManageService: DictManageService;

  @Post('/', { description: '新建' })
  async create(@Body(ALL) data: DictManageDTO) {
    return await this.dictManageService.create(data.toEntity());
  }

  @Put('/', { description: '编辑' })
  async edit(@Body(ALL) data: DictManageDTO) {
    const dictManage = await this.dictManageService.getById(data.id);
    const { desc, name, code, sort, type } = data;
    dictManage.desc = desc;
    dictManage.name = name;
    dictManage.code = code;
    dictManage.sort = sort;
    dictManage.type = type;
    // update
    return await this.dictManageService.edit(dictManage);
  }

  @Del('/:id', { description: '删除' })
  async remove(@Param('id') id: string) {
    const dictManage = await this.dictManageService.getById(id);
    await this.dictManageService.remove(dictManage);
  }

  @Get('/:id', { description: '根据id查询' })
  async getById(@Param('id') id: string) {
    return await this.dictManageService.getById(id);
  }

  @Get('/page', { description: '分页查询' })
  async page(@Query('page') page: number, @Query('size') size: number) {
    return await this.dictManageService.page(page, size);
  }

  @Get('/list', { description: '查询全部' })
  async list(@Query('type') type: string) {
    return await this.dictManageService.list({ type: type || null });
  }

  @Get('/opts', { description: '字典下拉选项' })
  async getDictOptions(@Query('type') type: string) {
    return await this.dictManageService.getDictOptions(type);
  }
}